#!/bin/bash

source /home/shell/util.sh

printInfo "版本列表："
curl -sL https://golang.google.cn/dl/|grep "toggleVisible"|perl -pe "s/.*go(.*)\".*/\1/g"
while [ true ]; do
    read -p "选择版本：" version
    if [[ -z "${version}" ]]; then
        printFail "请重新输入版本！"
        continue;
    else
        break;
    fi
done

if [ ! -f "/opt/go${go_version}.linux-amd64.tar.gz" ];then
  download /opt/go.tar.gz https://storage.googleapis.com/golang/go${version}.linux-amd64.tar.gz
fi
tar -C /usr/local/ -xzf /opt/go.tar.gz

mkdir -p $HOME/work/bin

tee /etc/profile.d/go_lang.sh<<EOF
export GOROOT=/usr/local/go
export PATH=\$PATH:\$GOROOT/bin
export GOPATH=\$HOME/work
export GOBIN=\$HOME/work/bin
EOF

source /etc/profile.d/go_lang.sh

# 清理
rm -rf /opt/go.tar.gz

# 显示信息
printInfo "go版本：`go version`"
printWarn "提示：请退出当前会话重新登录以使得GO生效！"